<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Helper_Common {
    /**
     * 服务端分页程序
     * @param int $records
     * @param int $size
     * @param int $page
     * @param string $qstring
     * @param bool $ajax
     * @param int $num
     * @param bool $jump
     */
    static function print_pager($records = 0, $size = 10, $page = 1, $qstring = null, $full_qstring = true, $ajax = false, $num = 5, $jump = true, $btn = array()) {
        //       echo $full_qstring;
        if(!$btn) {
            $pager = new Helper_Pager('首页', '上页', '下页', '尾页', '');
        }
        else {
            $pager = new Helper_Pager($btn['first'], $btn['prev'], $btn['next'], $btn['last'], '');
        }

        $pager -> NumCount = $num;
        $pager -> records = $records;
        $pager -> pagesize = $size;
        $pager -> jumpbox = $jump;
        $pager -> full_qstring = $full_qstring;
        $pager -> ajax = $ajax;
        if($page == 'this.value') {$pager -> page = 2;}
        else {
            $pager -> page = $page;
        }
        $pager -> Qstring = $qstring;
        echo $pager -> nav_print();
    }

    static function runtime() {
        global $g_boot_time;
        $shutdown_time = microtime(true);
        return sprintf('%0.2f', $shutdown_time - $g_boot_time);
    }

    static function currentStyle() {
        if(!$_COOKIE[Q::ini('session_cookie_name').'style']) $_COOKIE[Q::ini('session_cookie_name').'style'] = 'default';
        if(!$_COOKIE[Q::ini('session_cookie_name').'css']) $_COOKIE[Q::ini('session_cookie_name').'css'] = 'style';

//                dump($_COOKIE);
                return $_COOKIE[Q::ini('session_cookie_name').'style'];
//        return Q::ini('appini/front/view/style');
    }

    static function iniFront($ini) {
        return Q::ini('appini/front/'.$ini);
    }

    static function iniBack($ini) {
        return Q::ini('appini/background/'.$ini);
    }

    static function ini($ini) {
        return Q::ini('appini/'.$ini);
    }

    static function onestuff($key = null) {
        $condi[] = new QDB_Expr(
            "`key` = '{$key}'"
        );
        //        $stuff = Somestuff::find($condi)->one()->query();
        //        dump($stuff);
        $stuff = Somestuff::find($condi)->one()->query();
        if(!$stuff -> id()) $stuff = (object)array('value'=>'');
        return $stuff;
    }

    static function getFormCache($cache_id = '', $obj = null, $ref_id = null, $primary_key = 'cate_id', $parent_id = 'parent_id', $childrens = 'childrens') {
        $Class = QDB_ActiveRecord_Meta::instance($obj);
        if(!$cache_id) $cache_id = $obj.'_cache_';
        $refs = Helper_Cache::read($cache_id);
        //        $refs = null; //debug
        //        dump($refs);
        if(!$refs) {
            //                        echo 'caaa';
            $columns = $Class->find()->all()->query()->toArray();
            //                          exit();

            $tree = Helper_Array::toTree($columns, $primary_key, $parent_id, $childrens, $refs);
            Helper_Cache::write($cache_id, $refs);
        }
        if(!$ref_id) {
            return $refs;
        }
        return $refs[$ref_id];
    }

}
?>
